$.ajaxSetup({
	contentType : "application/x-www-form-urlencoded;charset=utf-8",
	cache : false,
	complete : function(data, status) {
		// 对返回的数据data做判断，
		// session过期的话，就location到一个页面
		switch(data.status){
			case 401:
			case 403:
			case 404:
				window.location.href = "/xjnt/login";
				break;
			default:
				break;
		}
	}
});

function tipMsg() {

	this.setPosition = function(pos) {
		Messenger.options = {
			extraClasses : 'messenger-fixed messenger-theme-flat messenger-on-bottom messenger-on-right'
		};
	};

	this.setMessage = function(msg, msgType) {
		Messenger().post({
			message : msg,
			type : msgType,
			hideAfter : 3,
			hideOnNavigate : true,
			showCloseButton : true
		});
	};

	this.success = function(msg, pos) {
		this.setPosition(pos);
		this.setMessage(msg, "success");
	};

	this.error = function(msg, pos) {
		this.setPosition(pos);
		this.setMessage(msg, "error");
	};

};
var TipMsg = new tipMsg();

/**
 * 查询列表页面初始化
 */
function initTableList(gridId, url, searchAreaId, searchBtnId) {
	var options = {
		url : url,
		method : 'post',
		contentType : 'application/x-www-form-urlencoded',
		pagination : true,
		sidePagination : 'server',
		pageNumber : 1,
		pageSize : 10,
		pageList : [ 10, 20, 30 ],
		paginationFirstText : '首页',
		paginationPreText : '上页',
		paginationNextText : '下页',
		paginationLastText : '末页',
		queryParamsType : '', // 不设置为limit
		iconSize : 'sm',
		iconsPrefix : 'ace-icon',
		icons : {
			refresh : 'fa fa-refresh',
			columns : 'fa fa-th'
		},
		queryParams : function(params) {
			var finalParams = {};
			if (searchAreaId !== null) {
				finalParams = $.extend({}, params, getParams(searchAreaId));
			}
			return finalParams;
		}
	};
	var $table = $('#' + gridId).bootstrapTable(options);
	if (searchBtnId !== null) {
		$('#' + searchBtnId).click(function() {
			$table.bootstrapTable('selectPage', 1);
		});
	}
}

$('#modal').on('hidden.bs.modal', function(e) {
	$(this).find('.modal-content').empty();
});

function loadModal(url, func) {
	$('#modal').find('.modal-content').load(url, func);
}

function changeModalTitle(title) {
	$('#modal').find('.modal-title').text(title);
}

function showModal() {
	$('#modal').modal('show');
}

function hideModal() {
	$('#modal').modal('hide');
}

/**
 * 获取表单参数
 * 
 * @param formId
 * @returns {}
 */
function getParams(formId) {
	var params = {};
	var formParams = $('#' + formId).serializeArray();
	$.each(formParams, function(name, val) {
		if (val.value !== '') {
			if(params[val.name] !== undefined){
				if(!params[val.name].push){
					params[val.name] = [params[val.name]];
				}
				params[val.name].push(val.value);
			} else {
				params[val.name] = val.value;
			}
		}
	});
	return params;
}

function goAdd(title, url, gridId) {
	loadModal(url, function(data, status) {
		if (status === "success") {
			changeModalTitle(title);
			showModal();
		} else if (status === "timeout") {
			TipMsg.error("请求超时，请重试!!!", 0);
		}
	});
}

function doAdd(url, formId, gridId, callback) {
	if(!validate(formId)){
		return false;
	}
	var params = getParams(formId);
	$.ajax({
		type : "POST",
		url : url,
		data : params,
		success : function(data) {
			if (data.status == "OK") {
				$("#" + gridId).bootstrapTable('refresh');
				hideModal();
				TipMsg.success(data.message, 0);
				if(callback){
					callback();
				}
			} else if (data.status == "ERROR") {
				TipMsg.error(data.message, 0);
			} else {
				var errors = data.errors;
				var msg = "";
				for ( var key in errors) {
					msg = errors[key];
				}
				TipMsg.error(msg, 0);
			}
		}
	});
}

function goUpdate(title, url, gridId) {
	var selections = $('#' + gridId).bootstrapTable("getSelections");
	if (selections !== undefined && selections !== null) {
		if (selections.length === 1) {
			var id = selections[0].uuid;
			url = url.indexOf('?') == -1 ? url + '?id=' + id : url + '&id='
					+ id;
			loadModal(url, function(data, status) {
				if (status === "success") {
					changeModalTitle(title);
					showModal();
				} else if (status === "timeout") {
					TipMsg.error("请求超时，请重试!!!", 0);
				}
			});
		} else {
			TipMsg.error("请选择一条数据记录进行编辑.", 0);
		}
	} else {
		TipMsg.error("没有可编辑的数据记录.", 0);
	}
}

function doUpdate(url, formId, gridId, callback) {
	if(!validate(formId)){
		return false;
	}
	var params = getParams(formId);
	$.ajax({
		type : "POST",
		url : url,
		data : params,
		success : function(data) {
			if (data.status == "OK") {
				$("#" + gridId).bootstrapTable('refresh');
				hideModal();
				TipMsg.success(data.message, 0);
				if(callback){
					callback();
				}
			} else {
				TipMsg.error(data.message, 0);
			}
		}
	});
}

function doDel(url, id, gridId, callback) {
	var isOk = confirm('您确定要删除数据记录吗?');
	if (isOk) {
		$.ajax({
			type : "POST",
			url : url,
			data : {
				id : id
			},
			success : function(data) {
				if (data.status == "OK") {
					$("#" + gridId).bootstrapTable('refresh');
					TipMsg.success(data.message, 0);
					if(callback){
						callback();
					}
				} else {
					TipMsg.error(data.message, 0);
				}
			}
		});
	}
}

function doDels(url, gridId, callback) {
	var selections = $('#' + gridId).bootstrapTable("getSelections");
	if (selections !== undefined && selections !== null) {
		if (selections.length > 0) {
			var ids = "";
			for (var i = 0; i < selections.length; i++) {
				ids += selections[i].uuid;
				if (i < selections.length - 1) {
					ids += ",";
				}
			}
			var isOk = confirm('您确定要删除当前选中的数据记录吗?');
			if (isOk) {
				$.ajax({
					type : "POST",
					url : url,
					data : {
						ids : ids
					},
					success : function(data) {
						if (data.status == "OK") {
							$("#" + gridId).bootstrapTable('refresh');
							TipMsg.success(data.message, 0);
							if(callback){
								callback();
							}
						} else {
							TipMsg.error(data.message, 0);
						}
					}
				});
			}
		} else {
			TipMsg.error("请至少选择一条数据记录.", 0);
		}
	} else {
		TipMsg.error("没有可删除的数据记录.", 0);
	}
};